import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test30 {

/*缺点：
    运用了两个for循环运行速度慢
*/
    public static void main(String[] args) {

        String s="wordgoodgoodgoodbestword";
        String[] words = {"word","good","best","word"};

        findSubstring(s,words);
    }
    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> res = new ArrayList<>();
        String wordSum=null;
        for (int i=0;i<words.length;i++){
            wordSum +=words[i];
        }
        wordSum=wordSum.substring(4);
        char[] chars = wordSum.toCharArray();
        System.out.println(chars);
        List<String> wordList = Arrays.asList(String.valueOf(chars));
        Collections.sort(wordList);
        System.out.println(wordList.toString());
        //有几个单词
        int wordsLen = words.length;
        //每个单词长度
        int wordLen = words[0].length();


        //s 中的 串联子串 是指一个包含  words 中所有字符串以任意顺序排列连接起来的子串 ,所以总长度
        int wordsLenSum = wordsLen * wordLen;
        if (s.length() < wordsLenSum) {
            return res;
        }
        for (int i = 0; i <= s.length() - wordsLenSum; i++) {


            String subStr = s.substring(i, i + wordsLenSum);
            String subsubStr=subStr.substring(0,wordLen);

            List<String> tmpList = new ArrayList<>();

            if (wordList.contains(subsubStr)){


                for (int k = 0; k < subStr.length(); k = k + wordLen) {
                    tmpList.add(subStr.substring(k, k + wordLen));
                    System.out.println(tmpList.toString());
                }
                Collections.sort(tmpList);
                if (tmpList.equals(wordList)) {
                    res.add(i);
                }
            }



        }
        return res;

    }
}
